package ystp.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import ystp.model.Ysrenyuanfenpei;
import ystp.model.Ysuser;
import ystp.service.KangjunyaowuService;
import ystp.service.KangjunyaowujieguoService;
import ystp.service.YsrenyuanfenpeiService;
import ystp.service.YsuserService;
import ystp.support.JsonResult;
import ystp.support.PageInfo;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("kangjunyaowu")
public class KangjunyaowuController {
    @Resource
    KangjunyaowuService ddService;

    @Resource
    KangjunyaowujieguoService ddKangjunyaowujieguoService;

    @Resource
    YsrenyuanfenpeiService ddYsrenyuanfenpeiService;

    @Resource
    YsuserService ddYsuserService;

    public Logger log = LoggerFactory.getLogger(KangjunyaowuController.class);


    @RequestMapping("findkangjunyaowulist")
    public JsonResult<?> findKangjunyaowulist(@RequestParam("xiangmuid") String ppxiangmuId, @RequestParam("pageindex") int ppPageIndex,
                                               @RequestParam("pagesize") int ppPageSize){
        try {
            List<Map<String,Object>> mmKangjunList = new ArrayList<>();
            List<Map<String,Object>> mmPinzhongAll = ddService.findPinzhongAll(ppxiangmuId,"","","","","","",ppPageIndex,ppPageSize);
            for (int i = 0; i < mmPinzhongAll.size(); i++) {
                int mmPinzhongHebingshu = 0;
                Map<String,Object> mmMap = mmPinzhongAll.get(i);
                if (mmMap!=null){
                    String mmPinzhongOne = mmMap.get("pinzhong") == null ? "" : mmMap.get("pinzhong").toString();
                    List<Map<String,Object>> mmPinguilist = ddService.findPinguiByPinzhong(ppxiangmuId,mmPinzhongOne);
                    if (mmPinguilist.size()>0){
                        mmPinzhongHebingshu = mmPinguilist.size();
                    }else{
                        mmPinzhongHebingshu = mmPinzhongHebingshu+1;
                    }
                    mmMap.put("pinguilist",mmPinguilist);
                    mmMap.put("pinzhonghebinghang",mmPinzhongHebingshu);
                }
                mmKangjunList.add(mmMap);
            }
            Integer mmCount = ddService.getPinzhongCount(ppxiangmuId,"","","","","","",ppPageIndex,ppPageSize);
            PageInfo mmPageInfo = new PageInfo(ppPageIndex, ppPageSize, mmCount);

            Map<String, Object> mmReturnMap = new HashMap<String, Object>();
            mmReturnMap.put("KangjunyaowuList", mmKangjunList);
            mmReturnMap.put("PageInfo", mmPageInfo);
            return JsonResult.getSuccessResult(mmReturnMap);

        }catch (Exception e) {
            e.printStackTrace();
            log.error("kangjunyaowu/findkangjunyaowulist:error",e);
            return JsonResult.getErrorResult("kangjunyaowu/findkangjunyaowulist:error " + e.getMessage());
        }

    }

    @RequestMapping("findBiaojuelist")
    public JsonResult<?> findBiaojuelist(@RequestParam("xiangmuid") String ppxiangmuId,
                                         @RequestParam("userid") String ppuserId,

                                         @RequestParam("drugname") String ppdrugname,
                                         @RequestParam("fenlei") String ppfenlei,
                                         @RequestParam("jiyao") String ppjiyao,
                                         @RequestParam("yizhipingjia") String ppyizhipingjia,
                                         @RequestParam("yuanze") String ppyuanze,
                                         @RequestParam("jicaiyao") String ppjicaiyao,

                                         @RequestParam("pageindex") int ppPageIndex,
                                         @RequestParam("pagesize") int ppPageSize){
        try {
            List<Map<String,Object>> mmKangjunList = new ArrayList<>();
            List<Map<String,Object>> mmPinzhongAll = ddService.findPinzhongAll(ppxiangmuId,ppdrugname,ppfenlei,ppjiyao,ppyizhipingjia,ppyuanze,ppjicaiyao,ppPageIndex,ppPageSize);
            for (int i = 0; i < mmPinzhongAll.size(); i++) {
                int mmPinzhongHebingshu = 0;
                Map<String,Object> mmMap = mmPinzhongAll.get(i);
                if (mmMap!=null){
                    String mmPinzhongOne = mmMap.get("pinzhong") == null ? "" : mmMap.get("pinzhong").toString();
                    List<Map<String,Object>> mmPinguilist =ddService.findPinguiResultByPinzhong(ppxiangmuId,ppuserId,mmPinzhongOne,ppdrugname,ppfenlei,ppjiyao,ppyizhipingjia,ppyuanze,ppjicaiyao);
                    if (mmPinguilist.size()>0){
                        mmPinzhongHebingshu = mmPinguilist.size();
                    }else{
                        mmPinzhongHebingshu = mmPinzhongHebingshu+1;
                    }
                    mmMap.put("pinguilist",mmPinguilist);
                    mmMap.put("pinzhonghebinghang",mmPinzhongHebingshu);
                }
                mmKangjunList.add(mmMap);
            }
            Integer mmCount = ddService.getPinzhongCount(ppxiangmuId,ppdrugname,ppfenlei,ppjiyao,ppyizhipingjia,ppyuanze,ppjicaiyao,ppPageIndex,ppPageSize);
            PageInfo mmPageInfo = new PageInfo(ppPageIndex, ppPageSize, mmCount);

            Map<String, Object> mmReturnMap = new HashMap<String, Object>();
            mmReturnMap.put("KangjunyaowuList", mmKangjunList);
            mmReturnMap.put("PageInfo", mmPageInfo);
            return JsonResult.getSuccessResult(mmReturnMap);

        }catch (Exception e) {
            e.printStackTrace();
            log.error("kangjunyaowu/findBiaojuelist:error",e);
            return JsonResult.getErrorResult("kangjunyaowu/findBiaojuelist:error " + e.getMessage());
        }
    }


    @RequestMapping("getkangjunAllCount")
    public JsonResult<?> getkangjunAllCount(@RequestParam("xiangmuid") String ppxiangmuId){
        try {

            Integer mmTotal=ddService.findAllByXiangmuId(ppxiangmuId).size();
            Map<String, Object> mmMap = new HashMap<String, Object>();
            mmMap.put("total", mmTotal);
            return JsonResult.getSuccessResult(mmMap);
        }catch (Exception e) {
            e.printStackTrace();
            log.error("kangjunyaowu/getkangjunAllCount:error",e);
            return JsonResult.getErrorResult("kangjunyaowu/getkangjunAllCount:error " + e.getMessage());
        }
    }

    @RequestMapping("getfenleilist")
    public JsonResult<?> getfenleilist(@RequestParam("xiangmuid") String ppxiangmuId){
        try {
            List<Map<String,Object>> mmList=ddService.findFeileiByXiangmuId(ppxiangmuId);

            return JsonResult.getSuccessResult(mmList);
        }catch (Exception e) {
            e.printStackTrace();
            log.error("kangjunyaowu/getfenleilist:error",e);
            return JsonResult.getErrorResult("kangjunyaowu/getfenleilist:error " + e.getMessage());
        }
    }

    @RequestMapping("getjindulist")
    public JsonResult<?> getjindulist(@RequestParam("xiangmuid") String ppxiangmuId){
        try {
            List<Map<String,Object>> mmJindulist = new ArrayList<>();

            Integer mmTotal=ddService.findAllByXiangmuId(ppxiangmuId).size();
            List<Map<String, Object>> mmUserList=ddYsrenyuanfenpeiService.findRenyuanFenpeiByXiangmuId(ppxiangmuId);
            for(Map<String, Object> ysrenyuanfenpei:mmUserList){
                Map<String, Object> mmMap = new HashMap<String, Object>();
                String mmUserId=ysrenyuanfenpei.get("userid").toString();
                Ysuser mmYsuser=ddYsuserService.findOne(mmUserId);
                String mmUserName=mmYsuser.getUsername();
//                Integer mmBaoliushu=ddKangjunyaowujieguoService.getKangJunResultCount(ppxiangmuId,mmUserId,"1");
//                Integer mmTaotaishu=ddKangjunyaowujieguoService.getKangJunResultCount(ppxiangmuId,mmUserId,"0");
//                Integer mmWeibiaojueshu=mmTotal-mmBaoliushu-mmTaotaishu;

                Integer mmpinzhongshu=ddKangjunyaowujieguoService.getVoitedPinzhongList(ppxiangmuId,mmUserId).size();
                Integer mmpinguishu=ddKangjunyaowujieguoService.getKangJunResultCount(ppxiangmuId,mmUserId,"1");
                int mmjicaiyaoshu=ddKangjunyaowujieguoService.getGuizeCount(mmUserId,ppxiangmuId,"1");//规则1为集采药
                int mmtotaljicaiyaoshu=ddService.findAllByXiangmuIdAndGuizebianhao(ppxiangmuId,"1").size();
                String mmShifoutijiao="";
                if("1".equals(ysrenyuanfenpei.get("issubmit").toString())){
                    mmShifoutijiao="是";
                }else {
                    mmShifoutijiao="否";
                }
                mmMap.put("username", mmUserName);
                mmMap.put("pinzhongshu", mmpinzhongshu+"/48");
                mmMap.put("pinguishu", mmpinguishu+"/79");
                mmMap.put("jicaishu", mmjicaiyaoshu+"/"+mmtotaljicaiyaoshu);
                mmMap.put("shifoutijiao", mmShifoutijiao);
                mmJindulist.add(mmMap);
            }

            return JsonResult.getSuccessResult(mmJindulist);
        }catch (Exception e) {
            e.printStackTrace();
            log.error("kangjunyaowu/getjindulist:error",e);
            return JsonResult.getErrorResult("kangjunyaowu/getjindulist:error " + e.getMessage());
        }
    }


    @RequestMapping("delete")
    public JsonResult<?> delete(@RequestParam("xiangmuid") String ppxiangmuId){
        try {

            return ddService.deleteByxiangmuId(ppxiangmuId)==true? JsonResult.getSuccessResult("删除材料成功")
                    : JsonResult.getErrorResult("删除材料失败");

        }catch (Exception e) {
            e.printStackTrace();
            log.error("kangjunyaowu/delete:error",e);
            return JsonResult.getErrorResult("kangjunyaowu/delete:error " + e.getMessage());
        }
    }


}
